<?php
session_start();
require_once('../../class/validateData.php');
require_once('../../AjaxCall_Action_Utility/Login.class.php');
require_once('../../Connections/level2/L2connect.php');
$log = new KLogger("../../log/log_login.txt", KLogger::INFO);
 
$submitCheck = isset(filter_input(INPUT_POST, 'Submit'));
$emailCheck = isset(filter_input(INPUT_POST, 'email',FILTER_SANITIZE_EMAIL));
$passwdCheck = isset(filter_input(INPUT_POST, 'passwd',FILTER_SANITIZE_MAGIC_QUOTES));
$email = $emailCheck ? trim(filter_input(INPUT_POST, 'email',FILTER_SANITIZE_EMAIL)) : "";
$passwd = $passwdCheck ? trim(filter_input(INPUT_POST, 'passwd',FILTER_SANITIZE_MAGIC_QUOTES)) : "";
if ($submitCheck && $emailCheck && $passwdCheck) {
    if (verify_email($email)) {
        $login = new login();
        if ($login->checkrecord($email, $passwd)) {
            $_SESSION['user_id'] = $login->getuser_id();
            $_SESSION['grouptype'] = $login->getgrouptype();
            $_SESSION['name'] = $login->getfullname();
            $login->close();
            $log->LogInfo("$email : login");
            header("location: ../../index_admin.php");
            exit();
        } else {
            $login->close();
            $log->LogInfo("$email : login fail");
            header("location: ../../index.php?error=1");
            exit();
        }
    }
} else {
    $log->LogInfo("$email : login fail");
    header("location: ../../index.php?error=1");
    exit();
}
